﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OnlineJobPortal.Models;
using System.Data.Entity;
using System.Data;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.xml; 
namespace OnlineJobPortal.Controllers
{
    public class ResumeController : Controller
    {
        JobPortalEntities7 objEntities = new JobPortalEntities7();
        IResumeRepository resume;
        public ResumeController()
        { }
        public ResumeController(IResumeRepository resume)
        {
            this.resume = resume;
        }
        public ActionResult ResumeManagement()
        { 
            object value = Session["email"];
            if (value != null)
            {
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
            
        }
        public ActionResult EditResume()
        {
            object value = Session["email"];
            if (value != null)
            {
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
            
        }
        public ActionResult ViewResume()
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int id = (int)session_id; // int user_id
                ViewBag.InfoData = resume.SearchByInfoID(id);
                ViewBag.AcdmyData = resume.SearchByAcademicsID(id);
                ViewBag.Experiences = resume.SearchByExperienceID(id);
                ViewBag.Honours = resume.SearchByHonourID(id);
                ViewBag.lang = resume.SearchByLanguageID(id);
                ViewBag.Projects = resume.SearchByProjectID(id);
                ViewBag.References = resume.SearchByReferenceID(id);
                ViewBag.Skills = resume.SearchBySkillsID(id);
                ViewBag.TargetJob = resume.SearchByTargetJobID(id);
                ViewBag.ID = id;
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult DeleteResume()
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int id = (int)session_id; // int user_id
                ViewBag.InfoData = resume.SearchByInfoID(id);
                ViewBag.AcdmyData = resume.SearchByAcademicsID(id);
                ViewBag.Experiences = resume.SearchByExperienceID(id);
                ViewBag.Honours = resume.SearchByHonourID(id);
                ViewBag.lang = resume.SearchByLanguageID(id);
                ViewBag.Projects = resume.SearchByProjectID(id);
                ViewBag.References = resume.SearchByReferenceID(id);
                ViewBag.Skills = resume.SearchBySkillsID(id);
                ViewBag.TargetJob = resume.SearchByTargetJobID(id);
                ViewBag.ID = id; 
                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Delete1(int id)
        {
            object value = Session["email"];
            if (value != null)
            {
                resume.DeleteResume(id);
                return RedirectToAction("ResumeManagement");
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult PersonalInformation()
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int id = (int)session_id; // int user_id
                ViewBag.gender = null;
                ViewBag.martial = null;
                ViewBag.country = null;
                ViewBag.city = null;
                ViewBag.industry = null;
                ViewBag.ExperienceLevel = null;

                PersonalInfo pInfo = resume.SearchByInfo(id);
                if (pInfo != null)
                {
                    ViewBag.gender = pInfo.Gender;
                    ViewBag.martial = pInfo.Martial_Status;
                    ViewBag.country = pInfo.Country;
                    ViewBag.city = pInfo.City;
                    ViewBag.industry = pInfo.Industry;
                    ViewBag.ExperienceLevel = pInfo.ExperienceLevel;
                    return View(pInfo);
                }

                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult UpdateInfo(PersonalInfo pInfo)
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int id = (int)session_id; // int user_id
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    HttpPostedFileBase file = Request.Files[i];
                    file.SaveAs(Server.MapPath(@"~\Images\" + file.FileName));
                    pInfo.ProfilePicture = file.FileName;
                }
                if (pInfo.UserId == id)
                    resume.UpdateInfo(pInfo);
                else
                {
                    pInfo.UserId = id;
                    resume.AddInfo(pInfo);
                }
                

                return RedirectToAction("Academics");
            }
            return View("~/Views/Home/Index.cshtml");
        }
        
        public ActionResult Academics()
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int Userid = (int)session_id; // int user_id
                ViewBag.Degree_Level = null;
                ViewBag.Province = null;
                ViewBag.Country = null;
                ViewBag.City = null;

                Academic obj = resume.SearchByAcademics(Userid);
                if (obj != null)
                {
                    ViewBag.Degree_Level = obj.Degree_Level;
                    ViewBag.Province = obj.Province;
                    ViewBag.Country = obj.Country;
                    ViewBag.City = obj.City;
                    return View(obj);
                }

                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult UpdateAcademics(Academic acdmy)
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int userid = (int)session_id; // int user_id
                if (acdmy.UserID == userid)
                {
                    resume.UpdateAcademic(acdmy);
                }
                else
                {
                    acdmy.UserID = userid;
                    resume.AddAcademic(acdmy);
                }


                return RedirectToAction("Experiences");
            }
            return View("~/Views/Home/Index.cshtml");
        }
        
        public ActionResult Experiences()
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int Userid = (int)session_id; // int user_id
                ViewBag.Province = null;
                ViewBag.Country = null;
                ViewBag.City = null;

                Experience obj = resume.SearchByExperience(Userid);
                if (obj != null)
                {
                    ViewBag.Province = obj.Province;
                    ViewBag.Country = obj.Country;
                    ViewBag.City = obj.City;
                    return View(obj);
                }

                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult UpdateExperience(Experience exp)
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int userid = (int)session_id; // int user_id
                 if (exp.UserID == userid)
                     resume.UpdateExperience(exp);
                 else
                 {
                     exp.UserID = userid;
                     resume.AddExperience(exp);
                 }


                 return RedirectToAction("Projects");
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Projects()
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int Userid = (int)session_id; // int user_id
                 ViewBag.Province = null;
                 ViewBag.Country = null;
                 ViewBag.City = null;
                 ViewBag.ProjectType = null;

                 Project obj = resume.SearchByProject(Userid);
                 if (obj != null)
                 {
                     ViewBag.Province = obj.Province;
                     ViewBag.Country = obj.Country;
                     ViewBag.City = obj.City;
                     ViewBag.ProjectType = obj.ProjectType;
                     return View(obj);
                 }

                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult UpdateProject(Project project)
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int userid = (int)session_id; // int user_id
                if (project.UserID == userid)
                    resume.UpdateProject(project);
                else
                {
                    project.UserID = userid;
                    resume.AddProject(project);
                }


                return RedirectToAction("Languages");
            }
            return View("~/Views/Home/Index.cshtml");
        }

        public ActionResult Languages()
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int Userid = (int)session_id; // int user_id
                 ViewBag.Language1 = null;
                 ViewBag.ProficiencyLevel = null;

                 Language obj = resume.SearchByLanguage(Userid);
                 if (obj != null)
                 {
                     ViewBag.Language1 = obj.Language1;
                     ViewBag.ProficiencyLevel = obj.ProficiencyLevel;
                     return View(obj);
                 }

                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }

        public ActionResult UpdateLanguage(Language lang)
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int userid = (int)session_id; // int user_id
                if (lang.UserID == userid)
                    resume.UpdateLanguage(lang);
                else
                {
                    lang.UserID = userid;
                    resume.AddLanguage(lang);
                }
                return RedirectToAction("Skills");
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult Skills()
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int Userid = (int)session_id; // int user_id
                ViewBag.LastUsed = null;
                ViewBag.Level = null;
                ViewBag.Experience = null;
                Skill obj = resume.SearchBySkills(Userid);
                if (obj != null)
                {
                    ViewBag.LastUsed = obj.LastUsed;
                    ViewBag.Level = obj.Level;
                    ViewBag.Experience = obj.Experience;
                    return View(obj);
                }

                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult UpdateSkill(Skill skill)
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int userid = (int)session_id; // int user_id
                 if (skill.UserID == userid)
                     resume.UpdateSkills(skill);
                 else
                 {
                     skill.UserID = userid;
                     resume.AddSkills(skill);
                 }
                 return RedirectToAction("References");
             }
             return View("~/Views/Home/Index.cshtml");
        }

        public ActionResult References()
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int Userid = (int)session_id; // int user_id
                 ViewBag.Country = null;
                 ViewBag.City = null;
                 ViewBag.Province = null;
                 ViewBag.ReferenceType = null;
                 Reference obj = resume.SearchByReference(Userid);
                 if (obj != null)
                 {
                     ViewBag.Country = obj.Country;
                     ViewBag.City = obj.City;
                     ViewBag.Province = obj.Province;
                     ViewBag.ReferenceType = obj.ReferenceType;
                     return View(obj);
                 }

                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }

        public ActionResult UpdateReference(Reference refer)
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int userid = (int)session_id; // int user_id
                 if (refer.UserID == userid)
                     resume.UpdateReference(refer);
                 else
                 {
                     refer.UserID = userid;
                     resume.AddReference(refer);
                 }
                 return RedirectToAction("Honours");
             }
             return View("~/Views/Home/Index.cshtml");
        }

        public ActionResult Honours()
        {
            object value = Session["email"];
            if (value != null)
            {
                object session_id = Session["userid"];
                int userid = (int)session_id; // int user_id
                ViewBag.Country = null;
                ViewBag.City = null;
                ViewBag.Province = null;
                ViewBag.Type = null;
                Honour obj = resume.SearchByHonour(userid);
                if (obj != null)
                {
                    ViewBag.Country = obj.Country;
                    ViewBag.City = obj.City;
                    ViewBag.Province = obj.Province;
                    ViewBag.Type = obj.Type;
                    return View(obj);
                }

                return View();
            }
            return View("~/Views/Home/Index.cshtml");
        }

        public ActionResult UpdateHonour(Honour honour)
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int userid = (int)session_id; // int user_id
                 if (honour.UserID == userid)
                     resume.UpdateHonour(honour);
                 else
                 {
                     honour.UserID = userid;
                     resume.AddHonour(honour);
                 }

                 return RedirectToAction("TargetJobs");
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult TargetJobs()
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int userid = (int)session_id; // int user_id

                 TargetJob obj = resume.SearchByTargetJob(userid);
                 if (obj != null)
                 {
                     return View(obj);
                 }

                 return View();
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult UpdateTargetJob(TargetJob tjob)
        {
             object value = Session["email"];
             if (value != null)
             {
                 object session_id = Session["userid"];
                 int userid = (int)session_id; // int user_id
                 if (tjob.UserID == userid)
                     resume.UpdateTargetJob(tjob);
                 else
                 {
                     tjob.UserID = userid;
                     resume.AddTargetJob(tjob);
                 }

                 return RedirectToAction("ViewResume");
             }
             return View("~/Views/Home/Index.cshtml");
        }
        public ActionResult ResumeinPDF()
        {
        //    string name = "Rabia";
        //    Document document = new Document();

        //    try
        //    {
        //        PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/") + "pdf/" + "print.pdf", FileMode.Create));
        //        document.Open();
        //        using (StringReader stringreader = new StringReader(name))
        //        {
        //            List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(
        //                             stringreader, null);

        //            for (int k = 0; k < htmlarraylist.Count; k++)
        //            {
        //                document.Add((IElement)htmlarraylist[k]);

        //            }

        //            Paragraph mypara = new Paragraph();
        //            document.Add(mypara);

        //            document.Close();
        //        }

        //        Response.Redirect("~/pdf/print.pdf");
        //    }
        //    catch (Exception ex)
        //    {
                
        //    }
           return View();
        }

    }
}
